#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Author: Faker.Deng
Project 7 in Chapter 1 Problem 7
"""


# Get median
def get_median(src_list):
    """
    Ger median from a source list
    :param src_list: Input list
    :return: Median
    """
    new_list = sorted(src_list)
    length = len(new_list)
    if length % 2 == 0:
        max_index = int(length / 2)
        min_index = max_index - 1
        median = (new_list[min_index] + new_list[max_index]) / 2.0
    else:
        mid_index = int((length - 1) / 2)
        median = new_list[mid_index]
    return median


# Get mode
def get_mode(src_list):
    """
    Get mode from a source list
    :param src_list: Input list
    :return: Mode or None
    """
    count_dict = {}
    for element in src_list:
        if element in count_dict.keys():
            count_dict[element] += 1
        else:
            count_dict[element] = 1
    mode = []
    max_count = max(count_dict.values())
    if max_count == 1:
        print("No mode!")
        return
    else:
        for key, value in count_dict.items():
            if value == max_count:
                mode.append(key)
    return mode


# Get mean
def get_mean(src_list):
    """
    Get mean from a source list
    :param src_list: Input list
    :return: Mean
    """
    length = len(src_list)
    total = 0
    for i in src_list:
        total += i
    mean = total / float(length)
    return mean


if __name__ == "__main__":
    """
    Test
    """
    a = list(range(0, 11, 1))
    print("List =", a)
    print("Median =", get_median(a))
    b = [1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 9, 9]
    print("List =", b)
    print("Mode =", get_mode(b))
    c = list(range(1, 11))
    print("List =", c)
    print("Mean =", get_mean(c))
